﻿/*include:js\poll*/
var Chat=	{
Load:function()
	{
	fastCSharp.$Id('User').Value(fastCSharp.Query.user);
	this.Poll=fastCSharp.NewObject('Poll',{VerifyPath:''});
	this.Poll.OnMessage.Add(fastCSharp.ThisFunction(this,this.OnPoll));
	this.Login();
	this.HtmlEditor=fastCSharp.NewObject('HtmlEditor',{ElementId:'MessageEditor',PasteLinkAjaxCallName:'pub.CrawlTitle',PasteImageAjaxCallName:'pub.PasteImage'});
	this.HtmlEditor.Start();
	fastCSharp.$Id('MessageEditor').Display(1);
	},
Login:function()
	{
	this.User=fastCSharp.$Value('User').Trim();
	if(this.User)
		{
		fastCSharp.PageView.SkinValue.NotLogin.Set(0);
		fastCSharp.PageView.SkinValue.LoginUser.Set('正在登录 '+this.User);
		fastCSharp.Ajax(fastCSharp.ThisFunction(this,this.OnLogin),'user.Login',{user:this.User});
		}
	},
OnLogin:function(Value)
	{
	if(Value.__AJAXRETURN__)
		{
		this.Poll.Query.UserVersion=Value.version;
		fastCSharp.PageView.SkinValue.LoginUser.Set(this.User);
		fastCSharp.PageView.SkinValue.IsLogin.Set(1);
		this.SetUsers(Value.__AJAXRETURN__);
		this.HtmlEditor.SetHtml('大家好，我是 '+this.User);
		this.Send();
		this.Poll.Start(this.User);
		}
	else	{
		alert('用户已注册 '+this.User);
		fastCSharp.PageView.SkinValue.LoginUser.Set('');
		fastCSharp.PageView.SkinValue.NotLogin.Set(1);
		}
	},
SetUsers:function(Users)
	{
	fastCSharp.PageView.SkinValue.Users.Set(Users.Remove(this.User));
	},
OnPoll:function(Value)
	{
	if(Value.Message)
		{
		if(Value.Message.Users)
			{
			this.Poll.Query.UserVersion=Value.Message.UserVersion;
			this.SetUsers(Value.Message.Users);
			}
		if(Value.Message.Messages)	this.AddMessage(Value.Message.Messages);
		}
	else	this.Poll.Stop();
	},
AddMessage:function(NewMessages)
	{
	var Messages=fastCSharp.PageView.SkinValue.Messages;
	Messages.Set((Messages.SkinValue||[]).concat(NewMessages));
	},
Logout:function()
	{
	fastCSharp.Ajax(null,'user.Logout',{user:this.User});
	this.Poll.Stop();
	this.User=null;
	fastCSharp.PageView.SkinValue.Users.Set([]);
	fastCSharp.PageView.SkinValue.Messages.Set([]);
	fastCSharp.PageView.SkinValue.LoginUser.Set('');
	fastCSharp.PageView.SkinValue.IsLogin.Set(0);
	fastCSharp.PageView.SkinValue.NotLogin.Set(1);
	},
Send:function()
	{
	var CheckUsers=fastCSharp.$Name('UserCheckbox').GetElements().Find(function(CheckBox){return CheckBox.checked;});
	var Value={user:this.User,message:this.HtmlEditor.GetHtml(),users:(CheckUsers.length?CheckUsers.ToArray(function(CheckBox){return CheckBox.value;}):null)};
	fastCSharp.Ajax(null,'user.Send',Value);
	this.AddMessage([{User:this.User,Time:new Date,Message:Value.message}]);
	this.HtmlEditor.SetHtml('');
	}
		};
//Chat.Poll=fastCSharp.ThisFunction(Chat,Chat.Poll);
fastCSharp.OnModule(['htmlEditor'],fastCSharp.ThisFunction(Chat,Chat.Load));